// themedetail.js
var weburl;
var app = getApp()
var sceneres = {}
var shihan = require('../../common/fetch')
var config = require('../../common/config')
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    console.log(config)
    var that = this
    if (options.scene) {
      var scene = decodeURIComponent(options.scene).split('&'); //t=0&id=232&pid=1694
      if (scene && scene.length > 0) {
        for (var i = 0; i < scene.length; i++) {
          var str = scene[i].split('=')
          if (str[0] == 't') {
            this.setData({
              sharetype: str[1]
            })
          } else if (str[0] == 'id'){
            this.setData({
              shareid: str[1]
            })
          }else if (str[0] == 'pid') {
            config.partner_id = str[1]
          } else if (str[0] == 'cid') {
            config.cid = str[1]
          }
        }
      }
    } else {
      this.setData({
        sharetype: options.type,
        shareid: options.id
      })
      if (options.cid) {
        config.cid = options.cid
      }
      if (options.fansid) {
        config.partner_id = options.fansid
      }else if (options.pid) {
        config.partner_id = options.pid
      } else if (options.partner_id) {
        config.partner_id = options.partner_id
      }
    }
    var pages = getCurrentPages();
    if (pages.length >= 2) {
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        promotefresh: false
      })
    }
    if (this.data.sharetype == 0) {
      this.themeDetail(config.recommendeddetail, this.data.shareid)
    }
    config.specialpage = 1 //特殊页面，web-view,需要刷新src
    console.log(sceneres)
  },
  /**
 * 生命周期函数--监听页面初次渲染完成
 */
  onReady: function () {
    app.wechatSwitch().then(function (wechat_switch) {//是否打开微信授权开关
      app.commonLogin(sceneres, wechat_switch)
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    config.webresiter = 0
    var token = wx.getStorageSync('token');
    if (this.data.sharetype == 0) {
      this.setData({
        weburl: config.commonurl + '/h5/event.html?target_id=' + this.data.shareid + '&token=' + token
      })
    }
  },
  themeDetail: function (a, b) {
    var that = this
    shihan.sendRequest(a + b + '.html', 'GET', {}, {})
      .then(function (response) {
        that.setData({
          themedetail: response.data.data
        })
      }, function (error) {
        console.log(error);
      });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var that = this, sharetit = '', shareurl;
    var customerId = wx.getStorageSync('customerId');
    if (that.data.themedetail.sh_name) {
      sharetit = that.data.themedetail.sh_name
    }
    shareurl = '/pages/themedetail/themedetail?type=' + that.data.sharetype + '&id=' + that.data.shareid + '&pid=' + customerId
    // console.log(options.webViewUrl)
    return {
      title: sharetit,
      path: shareurl,
      // imageUrl: that.data.themedetail.sh_share_image,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})